package io.renren.common.utils;

import io.renren.common.constant.CommonCode;
import io.renren.common.constant.IResultCode;
import io.renren.common.constant.MsgCode;
import io.renren.common.exception.BaseException;
import io.renren.common.msg.Response;
import org.springframework.util.StringUtils;

import java.util.Collections;

/**
 * Created by czh on 2020/7/7 11:46
 *
 * @author chengzhenhua
 */
public class ResponseUtil {

    /**
     * 构建成功的结果
     *
     * @param t
     * @param <T>
     * @return
     */
    public static <T> Response<T> success(T t) {
        Response response = new Response();
        response.setCode(IResultCode.HANDLER_SUCCESS);
        response.setBusinessCode(CommonCode.OPER_BASE_SUCCESS.getCode());
        response.setMsg(CommonCode.OPER_BASE_SUCCESS.getMessage());
        response.setData(t == null ? Collections.EMPTY_MAP : t);
        return response;
    }

    /**
     * 构建成功的结果
     *
     * @param <T>
     * @return
     */
    public static <T> Response<T> success() {
        return success(null);
    }

    /**
     * 构建失败的结果
     *
     * @param t
     * @param <T>
     * @return
     */
    public static <T> Response<T> fail(T t) {
        Response response = new Response();
        response.setCode(IResultCode.HANDLER_FAILED);
        response.setBusinessCode(CommonCode.OPER_BASE_FAILD.getCode());
        response.setMsg(CommonCode.OPER_BASE_FAILD.getMessage());
        response.setData(t == null ? Collections.EMPTY_MAP : t);
        return response;
    }

    /**
     * 构建失败的结果
     *
     * @param <T>
     * @return
     */
    public static <T> Response<T> fail() {
        return fail(null);
    }

    /**
     * 构建失败的结果
     *
     * @param t
     * @param commonCode
     * @param <T>
     * @return
     */
    public static <T> Response<T> fail(T t, MsgCode commonCode) {
        Response response = new Response();
        response.setCode(IResultCode.HANDLER_FAILED);
        if (commonCode == null) {
            commonCode = CommonCode.OPER_BASE_FAILD;
        }
        response.setBusinessCode(commonCode.getCode());
        response.setMsg(commonCode.getMessage());
        response.setData(t == null ? Collections.EMPTY_MAP : t);
        return response;
    }

    /**
     * 构建失败的结果
     *
     * @param t
     * @param msg
     * @param <T>
     * @return
     */
    public static <T> Response<T> fail(T t, String msg) {
        Response response = new Response();
        response.setCode(IResultCode.HANDLER_FAILED);
        response.setBusinessCode(CommonCode.OPER_BASE_FAILD.getCode());
        response.setMsg(msg);
        response.setData(t == null ? Collections.EMPTY_MAP : t);
        return response;
    }

    /**
     * 构建失败的结果
     *
     * @param t
     * @param msgCode
     * @param msg
     * @param <T>
     * @return
     */
    public static <T> Response<T> fail(T t, MsgCode msgCode, String msg) {
        Response response = new Response();
        response.setMsg(msg);
        response.setBusinessCode(CommonCode.OPER_BASE_FAILD.getCode());
        if (msgCode != null) {
            response.setBusinessCode(msgCode.getCode());
            if (StringUtils.isEmpty(msg)) {
                response.setMsg(msgCode.getMessage());
            }
        }
        response.setCode(IResultCode.HANDLER_FAILED);
        response.setData(t == null ? Collections.EMPTY_MAP : t);
        return response;
    }

    /**
     * 判断是否成功
     *
     * @param response
     * @return
     */
    public static Boolean isSuccess(Response response) {
        return response != null && IResultCode.HANDLER_SUCCESS.equals(response.getCode());
    }

    /**
     * 如果成功 则返回对象 如果失败则返回null
     *
     * @param response
     * @param <T>
     * @return
     */
    public static <T> T getData(Response<T> response) {
        if (response != null) {
            if (isSuccess(response)) {
                return response.getData();
            } else {
                throw new BaseException(response.getMsg());
            }
        }
        return null;
    }
}